package com.tien.action;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.tien.utils.AbstractAction;

/**
 * @author Tien  E-mail:g.tianxin@gmail.com
 * @version Time：2011-6-25 下午01:44:50
 */
@Controller
@Scope("prototype")
public class NewsAction extends AbstractAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6405472008683320751L;
	private String url;
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		URL feedUrl = null;
		InputStream is = null;
		InputStreamReader inputStreamReader;
		StringBuffer stringBuffer = new StringBuffer();
		try {
			feedUrl = new URL(url);
			is = feedUrl.openStream();
			inputStreamReader = new InputStreamReader(is, "UTF-8");
			BufferedReader br = new BufferedReader(inputStreamReader);
			String s = "";
			while ((s = br.readLine()) != null) {
				stringBuffer.append(s);
			}
			String html = stringBuffer.toString();
			html = html.replace("document.write('", "");
			html = html.replace("');", "");
			response.getWriter().write(html);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException ioe) {
			}
		}
		return null;
	}
	
	
	public void parseNews2DB() throws IOException{
//		String url = "http://news.baidu.com/n?cmd=1&class=sportnews&pn=1&tn=newsbrofcu";
//		String path = request.getRealPath("")+ "/temp/temp.html";
//		File input = new File(path);
//		Document doc = Jsoup.parse(input, "UTF-8", url);
//
//		Elements content = doc.getElementsByClass("baidu");
//		Elements links = content.get(0).getElementsByTag("a");
//		for (Element link : links) {
//		  String linkHref = link.attr("href");
//		  String linkText = link.text();
//		  System.out.println(linkHref);
//		}
	}
	
	public static void main(String[] args) throws IOException{
		NewsAction a = new NewsAction();
		a.parseNews2DB();
	}


	public String getUrl() {
		return url;
	}


	public void setUrl(String url) {
		this.url = url;
	}
	

}
 